package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.IDepartmentService;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController// @RestController是 一个组合注解     @RestController=@ResponseBody+@Controller
@RequestMapping("/department")
@Api(tags = "部门管理所有的接口",description = "部门接口详细描述")
public class DepartmentController {


    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IEmployeeService employeeService;


    @PatchMapping//  相当于  @RequestMapping(value = "/department",method = RequestMethod.PATCH)
    @ApiOperation(value = "查询所有的部门",notes = "我们这个接口没有分页哦")
    public PageInfo<Department> findPageByQuery(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.findPageByQuery(departmentQuery);
    }


    @GetMapping("/{id}")// 相当于      @RequestMapping(value = "/department/{id}",method = RequestMethod.GET)
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }



   //新增和修改都走此方法
    @PostMapping// 相当于    @RequestMapping(value = "/department",method = RequestMethod.POST)
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult save(@RequestBody Department department){
        try {
            departmentService.save(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    //前端选中多个数据进行批量删除
    @PatchMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Department> departments){
        try {
            departmentService.batchRemove(departments);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    //根据id删除数据
    @DeleteMapping("/{id}")//相当于@RequestMapping(value = "/department/{id}",method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    //查询所有的员工
    @PatchMapping("/findManagers")
    public List<Employee> findManagers(){
        return employeeService.findAll();
    }

    //查询部门树
    @PatchMapping("/findDepartmentTree")
    public List<Department> findDepartmentTree(){
        return departmentService.findDepartmentTree();
    }

}
